# -*- coding: utf-8 -*-
"""
Created on Tue May 20 22:17:29 2014

@author: Jinku Hu
"""

from xml.dom import minidom
#import requests
from lxml import etree
from urllib2 import urlopen
import StringIO
import re
from datetime import datetime

Tnow = datetime.now()

MatchBettingPattern = re.compile(r'.*Match Betting$')

parser = etree.XMLParser(recover=True)
#link = 'http://api.openweathermap.org/data/2.5/weather?q=Delft,nl&mode=xml'
EUFootballMatchResultLink = 'http://cachepricefeeds.williamhill.com/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=46&marketSort=MR&filterBIR=N'
UKFootballMatchResultlink = 'http://cachepricefeeds.williamhill.com/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=MR&filterBIR=N'

r = urlopen(EUFootballMatchResultLink).read()

root = etree.fromstring(r)

#r = requests.get(link).text

xmldoc = minidom.parseString(r)

#xmldoc = etree.fromstring(r, parser)

#itemlist = xmldoc.getElementsByTagName('sun')

#list1 = xmldoc.getElementsByTagName('clouds')

#print itemlist[0].attributes['rise'].value
#print itemlist[0].attributes['set'].value

#print list1[0].attributes['value'].value

Leagues = xmldoc.getElementsByTagName('type')

# list all aviable league name
for League in Leagues:
    if League.hasAttribute('name'):
        print League.attributes['name'].value

for League in Leagues:
    if League.attributes['name'].value == 'Dutch Eredivisie':
        for match in League.getElementsByTagName('market'):
            if re.match(MatchBettingPattern, match.attributes['name'].value):
                MatchName = match.attributes['name'].value
                print MatchName
                MatchNameSplit = MatchName.split()
                HostTeam = MatchNameSplit[0]
                AwayTeam = MatchNameSplit[2]
                MatchDict = {}
                for odd in match.getElementsByTagName('participant'):
                    MatchDict.update({odd.attributes['name'].value: odd.attributes['oddsDecimal'].value})
                print MatchDict
                
                
    


